package cn.com.jsepc.rep.subject_pool;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.Hashtable;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;
import cn.com.jsepc.rep.catalog.CatalogDAO;
import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.ReportVO;
import cn.com.jsepc.rep.comm.Common;
import com.jarp.db.DAO;

public class subject_report {

    // 加载表格的数据
    public String getDemeGrid(String start, String limit, String KeyWord_0,
            String KeyWord_1, String KeyWord_2, String KeyWord_3,
            String KeyWord_4, String KeyWord_5, String subID) {
        String res = "";
        DAO dao = new DAO();
        try {
            List<ReportVO> lstResults = new ArrayList<ReportVO>();
            int iStart = Integer.parseInt(start);
            int iPageSize = Integer.parseInt(limit);
            int iCurPage = iStart / iPageSize + 1;
            StringBuffer str = new StringBuffer();
            str.append("select a.*,c.CATANAME,t.subjectID,d.NAME DEPTNAME  from (select * from TB_REPORT a where ISVALID = 1 ");
            if (KeyWord_5 != null && !KeyWord_5.equals("")) {
                //点击树根节点的情况
                if (KeyWord_5.equals("0")) {
                    if (KeyWord_0 != null && !KeyWord_0.equals("")) {
                        str.append(" and a.REPNAME like '%" + KeyWord_0 + "%'");
                    }
                    if (KeyWord_1 != null && !KeyWord_1.equals("") && !KeyWord_1.equals("101")) {
                        str.append(" and a.DEPARTMENT = '" + KeyWord_1 + "'");
                    }
                    if (KeyWord_2 != null && !KeyWord_2.equals("")) {
                        if (KeyWord_2.equals("3")) {
                        } else {
                            str.append(" and a.SOURCETYPE = " + KeyWord_2);
                        }
                    }
                    if (KeyWord_3 != null && !KeyWord_3.equals("")) {
                        if (KeyWord_3.equals("6")) {
                        } else {
                            str.append(" and a.DATATYPE = " + KeyWord_3);
                        }
                    }
                    if (KeyWord_4 != null && !KeyWord_4.equals("")) {
                        if (KeyWord_4.equals("7")) {
                        } else {
                            str.append(" and a.UPDATETIME = " + KeyWord_4);
                        }
                    }
                } //点击树子节点的情况
                else {
                    if (KeyWord_0 != null && !KeyWord_0.equals("")) {
                        str.append(" and a.REPNAME like '%" + KeyWord_0 + "%'");
                    }
                    if (KeyWord_1 != null && !KeyWord_1.equals("") && !KeyWord_1.equals("101")) {
                        str.append(" and a.DEPARTMENT = '" + KeyWord_1 + "' ");
                    }
                    if (KeyWord_2 != null && !KeyWord_2.equals("")) {
                        if (KeyWord_2.equals("3")) {
                        } else {
                            str.append(" and a.SOURCETYPE = " + KeyWord_2);
                        }
                    }
                    if (KeyWord_3 != null && !KeyWord_3.equals("")) {
                        if (KeyWord_3.equals("6")) {
                        } else {
                            str.append(" and a.DATATYPE = " + KeyWord_3);
                        }
                    }
                    if (KeyWord_4 != null && !KeyWord_4.equals("")) {
                        if (KeyWord_4.equals("7")) {
                        } else {
                            str.append(" and a.UPDATETIME = " + KeyWord_4);
                        }
                    }
                    if (KeyWord_5 != null && !KeyWord_5.equals("")) {
                        String catid = new CatalogDAO().getAllCatatID(KeyWord_5);
                        str.append(" and a.CATAID in ( " + catid + ") ");
                    }
                }
            } //未点击树的情况
            else {
                if (KeyWord_0 != null && !KeyWord_0.equals("")) {
                    str.append(" and REPNAME like '%" + KeyWord_0 + "%'");
                }
                if (KeyWord_1 != null && !KeyWord_1.equals("") && !KeyWord_1.equals("101")) {
                    str.append(" and DEPARTMENT = '" + KeyWord_1 + "'");
                }
                if (KeyWord_2 != null && !KeyWord_2.equals("")) {
                    if (KeyWord_2.equals("3")) {
                    } else {
                        str.append(" and SOURCETYPE = " + KeyWord_2);
                    }
                }
                if (KeyWord_3 != null && !KeyWord_3.equals("")) {
                    if (KeyWord_3.equals("6")) {
                    } else {
                        str.append(" and DATATYPE = " + KeyWord_3);
                    }
                }
                if (KeyWord_4 != null && !KeyWord_4.equals("")) {
                    if (KeyWord_4.equals("7")) {
                    } else {
                        str.append(" and UPDATETIME = " + KeyWord_4);
                    }
                }
            }
            str.append(" ) a");
            str.append(" left join TB_SUBJECT_REP t on t.subjectID=" + subID + " and t.repid = a.repid");
            str.append(" left join TB_CATALOG c on c.CATAID = a.CATAID");
            str.append(" left join TB_CORP_DEPT d on d.ID=a.DEPARTMENT ");

            Vector v = dao.getDataSet(ConstUtil.DBNAME, str.toString(), iCurPage, iPageSize);
            if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hashRows = (Hashtable) v.get(i);
                    ReportVO repVO = new ReportVO();
                    repVO.setRepid((String) hashRows.get("REPID"));
                    repVO.setRepcode((String) hashRows.get("REPCODE"));
                    repVO.setRepname((String) hashRows.get("REPNAME"));
                    repVO.setCataid((String) hashRows.get("CATANAME"));
                    repVO.setRepdesc((String) hashRows.get("REPDESC"));
                    repVO.setDesc((String) hashRows.get("CALDESC"));
                    repVO.setDataprec((String) hashRows.get("DATAPREC"));
                    repVO.setDataunit((String) hashRows.get("DATAUNIT"));
                    repVO.setDepartmentid((String) hashRows.get("DEPARTMENT"));
                    repVO.setDepartment((String) hashRows.get("DEPTNAME"));
                    repVO.setSourcetype((String) hashRows.get("SOURCETYPE"));
                    repVO.setDatatype((String) hashRows.get("DATATYPE"));
                    repVO.setUpdatetime((String) hashRows.get("UPDATETIME"));
                    repVO.setReffact((String) hashRows.get("REFFACT"));
                    repVO.setRepvaluecol((String) hashRows.get("REPVALUECOL"));
                    repVO.setIsselect((String) hashRows.get("SUBJECTID"));
                    lstResults.add(repVO);

                }
            }
            JSONArray json = new JSONArray();
            StringBuffer str1 = new StringBuffer();
            str1.append("select count(1) CNT from (");
            str1.append(str);
            str1.append(" ) z ");
            String totalCount = dao.getDataValue(ConstUtil.DBNAME, str1.toString());
            json = JSONArray.fromObject(lstResults);
            res = "{totalCount:" + totalCount + ",data:" + json.toString()
                    + "}";
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("NewProjectTree getXmDl Exception.");
        }
        return res;

    }

    //添加主题池的关联指标
    public synchronized String saveRole(String allarray, String selarray, String subID) throws ServletException {
        String ret = "";
        try {
            DAO dao = new DAO();
            ArrayList<String> sqls = new ArrayList<String>();
            Common comm = new Common();
            String[] repidArray = selarray.split(",");
            sqls.add("delete from tb_subject_rep  where subjectID=" + subID + " and repid in (" + allarray + ")");
            for (int i = 0; i < repidArray.length; i++) {
                String repid = repidArray[i];
                String refID = comm.getSeqStr("seq_subject_rep");
                String sql = "insert into  TB_SUBJECT_REP(" + "refID ," + "subjectID ," + "repID) values(" + refID + "," + subID + "," + repid + ")";
                sqls.add(sql);
            }
            if (sqls.size() > 0) {
                if (dao.execCommand("Jarp", sqls)) {
                    ret = "ok";
                    System.out.println("保存成功");
                } else {
                    ret = "fail";
                    System.out.println("保存失败");
                }
            } else {
                ret = "fail";
                System.out.println("no rowdata modify");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
